package com.binioter.guideview;

import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.utils.Rect;

/**
 * Created by binIoter
 *
 * @since 2021-04-19
 */
public class Common {
    /**
     * 设置Component
     *
     * @param inflater 引用布局资源
     * @param viewComment 控件id
     * @return view
     */
    static ComponentContainer componentToView(LayoutScatter inflater, viewComment viewComment) {
        final MaskView.LayoutParams lp = new MaskView.LayoutParams(ComponentContainer.LayoutConfig.MATCH_CONTENT,
                ComponentContainer.LayoutConfig.MATCH_CONTENT);
        lp.offsetX = viewComment.getXOffset();
        lp.offsetY = viewComment.getYOffset();
        lp.targetAnchor = viewComment.getAnchor();
        lp.targetParentPosition = viewComment.getFitPosition();
        ComponentContainer view = (ComponentContainer) viewComment.getView(inflater);
        view.setLayoutConfig(lp);
        return view;
    }

    /**
     * Rect在屏幕上去掉状态栏高度的绝对位置
     *
     * @param view view
     * @param parentX x轴
     * @param parentY y轴
     * @return rect
     */
    static Rect getViewAbsRect(Component view, int parentX, int parentY) {
        int[] loc = view.getLocationOnScreen();
        Rect rect = new Rect();
        rect.set(view.getLeft(), loc[1] - 330, loc[0] + view.getWidth(), loc[1] + view.getHeight() - 285);
        rect.setPivot(-parentX, -parentY);
        return rect;
    }
}
